package com.premedsidekick;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class HomePageActivity extends Activity {
	GridView gridview;
	SharedPreferences preferences;
	SharedPreferences.Editor editor;
	Dialog dialog;
	AlertDialog alert;
	boolean alert_state;
	@Override
	public void onCreate(Bundle bundle){
		super.onCreate(bundle);
		setContentView(R.layout.gridview_prototype);

		preferences = getSharedPreferences("user_settings", 0);
		editor = preferences.edit();
		final int state = preferences.getInt("accepted", 0);
			
		gridview = (GridView)findViewById(R.id.gridview_default);
		//System.out.println(gridview);
		MCATGridScheduleAdapter adapter = new MCATGridScheduleAdapter(this);
		gridview.setAdapter(adapter);
		gridview.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            	Intent intent = null;
        		if (position == 0) {
        			//System.out.println("New Profile Activity");
        			intent = new Intent(HomePageActivity.this, New_Profile.class); //bring up ProfilePage
        		}
        		else if (position == 1) {
        			//System.out.println("Courses");
        			//newPage = new Intent(this, MedAppSchoolReqActivity.class); //bring up SchoolRequire
        			intent = new Intent(HomePageActivity.this, Temp_HorizontalScrollView.class); //bring up SchoolRequire
        		}
        		else if (position == 2) {
        			//newPage = new Intent(this, MedAppExtrReqActivity.class); //bring up Extra Require
        			intent = new Intent(HomePageActivity.this, New_ExtracurricularRequirements.class); //bring up Extra Require
        		}
        		else if (position == 3) {
        			//awards
        			intent = new Intent(HomePageActivity.this, Awards.class);
        		}
        		else if (position == 4) {
        			intent = new Intent(HomePageActivity.this, Temporary_Employment.class);
        		}
        		else if (position == 5) {
        			intent = new Intent(HomePageActivity.this, MedAppMCATsectionActivity.class);
        		}
        		else if (position == 6) {
        			intent = new Intent(HomePageActivity.this, MedAppN2KActivity.class);
        		}
        		else if (position == 7) {
        			intent = new Intent(HomePageActivity.this, Interview.class);
        		}
        		else if (position == 8) {
        			intent = new Intent(HomePageActivity.this, AboutUs.class);
        		}
        		else if (position == 9) {
        			dialog = new Dialog(HomePageActivity.this);
        			dialog.setContentView(R.layout.underconstruction);
        			dialog.setCancelable(true);
        			dialog.setTitle("School List");
        			dialog.show();
        		}
        		
        		if (intent != null) 
        			startActivity(intent);
            }
        });
		
		
		final AlertDialog.Builder builder = new AlertDialog.Builder(this);
		
		String message = getResources().getString(R.string.disclaimer);
		CharSequence sequence = Html.fromHtml(message);
		builder.setTitle("Disclaimer");
		builder.setMessage(sequence);
		if (state >= 2) {
			builder.setCancelable(true);
		}
		if (state < 2) {
			builder.setCancelable(false);
			builder.setPositiveButton("I Agree", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					final int count = state + 1;
					editor.putInt("accepted", count);
					editor.commit();	     
				}
			});
				
		}
		
		alert = builder.create();
		alert_state = true;
		alert.show();
		
	}
	public class MCATGridScheduleAdapter extends BaseAdapter {
		Context context;
		LayoutInflater inflater;
		ViewHolder viewholder;
		
		public MCATGridScheduleAdapter(Context context) {
			this.context = context;
			inflater = LayoutInflater.from(context);
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return icon_text.length;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				viewholder = new ViewHolder();
				convertView = inflater.inflate(R.layout.home_page_one_grid, null);
				viewholder.textview = (TextView)convertView.findViewById(R.id.home_page_icon_text);
				viewholder.textview.setText(icon_text[position]);
				viewholder.imageview = (ImageView)convertView.findViewById(R.id.home_page_icon);
				viewholder.imageview.setImageResource(icons[position]);
			}
			else {
				viewholder = (ViewHolder)convertView.getTag();
				viewholder.textview.setText(icon_text[position]);
				viewholder.imageview.setImageResource(icons[position]);
			}
			convertView.setTag(viewholder);
			return convertView;
		}
		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}
		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}
		// references to our images
		private Integer[] icons = {
	            R.drawable.profile,
	            R.drawable.pre_reqs,
	            R.drawable.extracurricular, 
	            //R.drawable.employment,
	            R.drawable.awards,
	            R.drawable.employment,
	            R.drawable.mcat,
	            R.drawable.faqs,
	            //R.drawable.timeline,
	            R.drawable.interview,
	            R.drawable.about_us,
	            R.drawable.school_list
	    };

	    // references to our texts
	    private String[] icon_text = {
	            "Profile",
	            "PreReqs",
	            "Extra\nCurricular", 
	            //"Employment",
	            "Awards &\nScholarships", 
	            "Employment",
	            "MCAT�",
	            "FAQS",
	            //"Timeline",
	            "Interview",
	            "About Us",
	            "School List"
	    };
	}
	public class ViewHolder {
		ImageView imageview;
		TextView textview;
	}
}